/*
 * @Author: heinan
 * @Date: 2023-07-23 22:51:39
 * @Last Modified by: heinan
 * @Last Modified time: 2023-08-03 13:50:27
 */
"use strict";
const { Controller } = require("egg");
/**
 *  @Controller 资讯管理
 */
class Infos extends Controller {
  /**
   * @Summary 获取资讯列表
   * @Description 获取资讯列表
   * @Router GET /api/v1/infos
   * @Request query string name 资讯名称
   * @Request query Number status 资讯状态 可选参数 前端传递1，不传递显示所有status数据
   * @Request query Number currentPage 页码 默认 1
   * @Request query Number pageSize 单页数量 默认 10
   */
  async index() {
    const result = await this.ctx.service.infos.index(this.ctx.query);
    if (result.data.length) {
      this.ctx.body = {
        code: 1,
        msg: "查询成功！",
        ...result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        msg: "暂无数据！",
        data: [],
      };
    }
  }
  /**
   * @Summary 删除接口
   * @Description 根据资讯id删除接口
   * @Router DELETE /api/v1/infos/{id}
   * @Request path string *id
   */
  async destory() {
    const result = await this.ctx.service.infos.destory(this.ctx.params);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "删除成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "删除失败！",
      };
    }
  }
  /**
   * @Summary 更新资讯列表
   * @Description 根据资讯id,接口更新字段
   * @Router PUT /api/v1/infos/{id}
   * @Request path string *id
   * @Request body updateInfomation *body
   */
  async update() {
    const result = await this.ctx.service.infos.update({
      ...this.ctx.params,
      ...this.ctx.request.body,
    });
    if (result) {
      this.ctx.body = {
        code: 1,
        message: "修改成功！",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "修改失败！",
      };
    }
  }
  /**
   * @Summary 添加接口权限
   * @Description 添加接口权限
   * @Router POST /api/v1/infos
   * @Request body createInfomation *body
   */
  async create() {
    try {
      this.ctx.validate({
        uid: "string",
        title: "string",
        articel: "string",
      });
    } catch (err) {
      this.ctx.status = 400;
      return (this.ctx.body = {
        code: 0,
        error: err.errors,
      });
    }
    const result = await this.ctx.service.infos.create(this.ctx.request.body);
    if (result.affectedRows) {
      this.ctx.body = {
        code: 1,
        message: "新增成功!",
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "新增失败!",
      };
    }
  }
  /**
   * @Summary 资讯详情
   * @Description 根据资讯id获取资讯文章
   * @Router GET /api/v1/infos/{id}
   * @Request path string *id
   */
  async show() {
    const result = await this.ctx.service.infos.show(this.ctx.params);
    if (result) {
      this.ctx.body = {
        code: 1,
        message: "查询成功!",
        data: result,
      };
    } else {
      this.ctx.body = {
        code: 0,
        message: "暂无数据!",
      };
    }
  }
}

module.exports = Infos;

